<?php

namespace app\admin\controller;


use app\common\model\MaintainModel;
use think\facade\Request;
use think\Validate;

class MaintainController extends BaseController
{

    public function index()
    {
        $this->assign('device_cate', MaintainModel::DEVICE_CATE);
        return $this->fetch();
    }

    public function getlist()
    {
        $title      = Request::param("title");
        $device     = Request::param("device");
        $start_time = Request::param("start_time");
        $end_time   = Request::param("end_time");
        $page       = Request::param("page");
        $limit      = Request::param("limit");

        $where = [];
        if (mb_strlen($title, 'UTF-8') > 0) {
            $where[] = ['title', 'like', "%{$title}%"];
        }
        if (is_numeric($device)) {
            $where[] = ['device', '=', $device];
        }
        if ($start_time) {
            $start_time = strtotime($start_time);
            if ($start_time) {
                $where[] = ['start_time', '>=', $start_time];
            } else {
                $where[] = ['start_time', '=', 0];
            }
        }

        if ($end_time) {
            $end_time = strtotime($end_time);
            if ($end_time) {
                $where[] = ['end_time', '<=', $end_time];
            } else {
                $where[] = ['end_time', '=', 0];
            }
        }

        $list = MaintainModel::with('admin')->where($where)->page($page, $limit)->order('id', 'desc')->select();
        if (count($list) > 0) {
            $count = MaintainModel::count();

            foreach ($list as &$item) {
                $item->device_text = $item->device_text;
                $item->start_time  = $item->start_time > 0 ? date('Y-m-d H:i:s', $item->start_time) : '';
                $item->end_time    = $item->end_time > 0 ? date('Y-m-d H:i:s', $item->end_time) : '';
                $item->remind_time = $item->remind_time > 0 ? date('Y-m-d H:i:s', $item->remind_time) : '';
                $item->update_time = $item->update_time > 0 ? date('Y-m-d H:i:s', $item->update_time) : '';
            }

            return json(["code" => 0, "msg" => "", "data" => $list, "count" => $count]);
        }
        return self::bulidFail("未查询到相关数据");
    }

    public function add()
    {
        if (Request::isPost()) {
            $all_param = Request::param();

            $validate = new Validate([
                'title'      => 'require|length:4,20',
                'content'    => 'require|length:10,200',
                'device'     => 'require|in:1,2,3,4',
                'start_time' => 'require',
                'end_time'   => 'require',
            ], [
                "title"      => "维护标题在4-20个字符之间",
                "content"    => "维护内容在10-200个字符之间",
                "device"     => "维护端选择错误",
                "start_time" => "请选择维护时间",
                "end_time"   => "请选择维护时间",
            ]);

            if (!$validate->check($all_param)) {
                return self::bulidFail($validate->getError());
            }

            $maintain_model = new MaintainModel($all_param);

            $maintain_model->start_time  = strtotime($all_param['start_time']);
            $maintain_model->end_time    = strtotime($all_param['end_time']);
            $maintain_model->remind_time = !empty($all_param['remind_time']) ? strtotime($all_param['remind_time']) : 0;
            $maintain_model->update_time = time();
            $maintain_model->admin_id    = $this->admin_id;

            if ($maintain_model->save()) {
                // 记录日志
                $log_content = "添加站点维护，ID：{$maintain_model->id}，标题：{$maintain_model->title}";
                parent::addAdminLog($log_content);
                return self::bulidSuccess();
            }
            return self::bulidFail();
        }

        $this->assign('device_cate', MaintainModel::DEVICE_CATE);
        return $this->fetch();
    }


    public function edit()
    {
        $id             = Request::param('id');
        $maintain_model = MaintainModel::get($id);


        if (Request::isPost()) {
            $all_param = Request::param();

            if (!$maintain_model) {
                return self::bulidFail("维护信息不存在");
            }

            $validate = new Validate([
                'title'      => 'require|length:4,20',
                'content'    => 'require|length:10,200',
                'device'     => 'require|in:1,2,3,4',
                'start_time' => 'require',
                'end_time'   => 'require',
            ], [
                "title"      => "维护标题在4-20个字符之间",
                "content"    => "维护内容在10-200个字符之间",
                "device"     => "维护端选择错误",
                "start_time" => "请选择维护时间",
                "end_time"   => "请选择维护时间",
            ]);

            if (!$validate->check($all_param)) {
                return self::bulidFail($validate->getError());
            }
            $maintain_model->title       = $all_param['title'];
            $maintain_model->content     = $all_param['content'];
            $maintain_model->device      = $all_param['device'];
            $maintain_model->start_time  = strtotime($all_param['start_time']);
            $maintain_model->end_time    = strtotime($all_param['end_time']);
            $maintain_model->remind_time = !empty($all_param['remind_time']) ? strtotime($all_param['remind_time']) : 0;
            $maintain_model->update_time = time();
            $maintain_model->admin_id    = $this->admin_id;

            if ($maintain_model->save()) {
                // 记录日志
                $log_content = "修改站点维护，ID：{$maintain_model->id}，标题：{$maintain_model->title}";
                parent::addAdminLog($log_content);
                return self::bulidSuccess();
            }
            return self::bulidFail();
        }

        if (!$maintain_model) {
            $this->error('维护信息不存在');
        }

        $maintain_model->remind_time = $maintain_model->remind_time > 0 ? date('Y-m-d H:i:s', $maintain_model->remind_time) : '';

        $this->assign('device_cate', MaintainModel::DEVICE_CATE);
        $this->assign('info', $maintain_model);
        return $this->fetch();
    }


    /**
     * 删除
     */
    public function set_status()
    {
        $id     = Request::param('id', 0, 'intval');
        $status = Request::param('status', 0, 'intval');
        if ($id < 1 || !in_array($status, [0, 1])) {
            return self::bulidFail('参数错误');
        }

        $maintain_model = MaintainModel::get($id);
        if (!$maintain_model) {
            return self::bulidFail('信息不存在');
        }

        $maintain_model->status      = $status;
        $maintain_model->update_time = time();
        $maintain_model->admin_id    = $this->admin_id;

        if ($maintain_model->save()) {
            // 记录日志
            $log_content = "设置站点维护状态，ID：{$maintain_model->id}，状态：" . ($maintain_model->status == 1 ? '有效' : '失效');
            parent::addAdminLog($log_content);

            return self::bulidSuccess();
        }
        return self::bulidFail();
    }
}